package com.atguigu.democustomer230315.controller;

import com.atguigu.democustomer230315.bean.Customer;
import com.atguigu.democustomer230315.bean.ParamObJ;
import com.atguigu.democustomer230315.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@RestController
//@RequestMapping("say")
public class CustomerController {
    @Autowired
//    @Qualifier("s1")
    CustomerService customerService;


    //请求演示
    @RequestMapping(value = "say", method = RequestMethod.GET)
    public String sayHello() {
        System.out.println("123");
        return "Hello";
    }

    //get请求 键值对参数
    @GetMapping("customerName")
    public String customerByName(@RequestParam("name") String na
    ) {
//        System.out.println(na);
//        return "customer:" + na;
        //Mybatisplus给Service提供的方法 查询表里面所有的数据
        System.out.println(customerService.list());

        //删除数据
//        customerService.remove(new QueryWrapper<Customer>().eq("name", na));



        return customerService.customerByName(na);
    }

    //get请求 多个键值对参数
    @GetMapping("customerNameAge")
    public String customerByNameAge(@RequestParam("name") String na,
                                    @RequestParam("age") Integer age
    ) {
        customerService.saveCustomerWithNameAndAge(na,age);
        System.out.println(na + ":" + age);
        return "customer:" + na + ":" + age;
    }

    //get请求 多个键值对参数
    @GetMapping("customerById/{customerId}")
    public String customerById(@PathVariable("customerId") String id) {
        System.out.println("id:" + id);
        return "customer：" + id;
    }

    //POST请求 并利用bean对象接受请求体参数
    @PostMapping("saveCustomer")
    public String saveCustomer(@RequestBody Customer customer) {
        customerService.saveCustomerWithObj(customer);
        //使用Mybatisplus提供的方法来保存数据
//        customerService.saveOrUpdate(customer);
        System.out.println(customer);

        return "success";
    }

    //get请求 使用bean对象接受键值对参数
    @GetMapping("customerNameAgeObj")
    public String customerByNameAgeWithObj(ParamObJ paramObJ) {
        System.out.println(paramObJ);
        return "customer:" + paramObJ;
    }
}
